<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/all.conf.php');
require_once(__PHY_WEB.__DIR_ADM.'/admin.inc.php');
require_once(__PHY_LIB.'/class/uCategory.class.php');
require_once(__PHY_LIB.'/class/uWork.class.php');
require_once(__PHY_LIB.'/class/CmsWork.class.php');

$rReq = validate($_REQUEST, 'msg:u|action:e[list,edit,new,add,update,delete]');
$sMsg = $rReq['msg'];
$sAction = $rReq['action'];

if ('add' == $sAction) {
	do {
		$rP = validate($_POST, 'work_id:N');
		if (!$rP) { $sMsg = '传入参数错误'; break; }
		$oOne = new CmsWork();
		$oOne->set('location', 'footer');
		$oOne->set('work_id', $rP['work_id']);
		$oOne->set('rank', $oOne->calcNewRank());
		if ($x = $oOne->invalid()) {
			$sMsg = '出错啦。原因：'.$x; break;
		}
		elseif ($oOne->insert()) {
			$sMsg = '添加成功'; break;
		}
		else {
			$sMsg = '添加失败！！'; break;
		}
	} while (false);
	$sAction = 'list';
}
elseif ('update' == $sAction) {
	do {
		$rP = validate($_POST, 'id:N|rank:N|work_id:N');
		if (!$rP) { $sMsg = '传入参数错误'; break; }
		$oOne = new CmsWork($rP['id']);
		$oOne->set('work_id', $rP['work_id']);
		if (!$oOne->rankTo($rP['rank'])) { $sMsg = '更新序号失败！！'; break; }
		$sMsg = '修改成功';
	} while (false);
	$sAction = 'list';
}
elseif ('delete' == $sAction) {
	do {
		$rP = validate($_POST, 'id:N');
		if (!$rP) { $sMsg = '传入参数错误'; break; }
		$oOne = new CmsWork($rP['id']);
		if (!$oOne->delete()) { $sMsg = '删除失败！！'; break; }
		$sMsg = '删除成功';
	} while (false);
	$sAction = 'list';
}

if ('new' == $sAction) {
}
elseif ('edit' == $sAction) {
	$oOne = new CmsWork($_REQUEST['id']);
	$oTpl->assign('rOne', $oOne->getData());

	$oList = new CmsWorkList();
	$oList->cfgField("rank,work_id");
	$oList->cfgWhere("location='footer'");
	$oList->cfgOrder("rank");
	$oList->load();
	$oTpl->assign('rSlctRanks', $oList->fetchArray());
}
elseif ('list' == $sAction) {
	$oList = new uWorkList('cms_work cw INNER JOIN unit_work w ON w.id=cw.work_id');
	$oList->cfgField("cw.id,cw.rank,cw.work_id,w.title AS work_title");
	$oList->cfgWhere("cw.location='footer'");
	$oList->cfgOrder("cw.rank");
	$oList->load();
//@ echo $oList->mSql;
	while ($oOne = $oList->nextItem()) {
		;
	}
	$oTpl->assign('rList', $oList->fetchArray());
}

$oTpl->assign('sMsg', $sMsg);
$oTpl->assign('sAction', $sAction);
$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').'.tpl');
?>
